package com.loby.monitor.excess.local;

import com.alibaba.fastjson.JSONObject;
import lombok.Builder;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 排放口的报警条件
 * @author loby
 * @date 2021/5/12
 */
@Data
@Builder
public class LimberAlarmCondition implements Serializable {
    /**
     * key:污染因子代码
     * value：报警条件信息
     */
    Map<String,DataExcessCondition> conditions;
    public LimberAlarmCondition()
    {
        conditions = new HashMap<>();
    }
    public LimberAlarmCondition(Map<String,DataExcessCondition> map,boolean isExcessEnable,boolean isOverRangeEnable)
    {
        conditions = map;
    }


    transient  boolean isExcessEnable = false;
    transient  boolean isOverRangeEnable = false;
    public void put(DataExcessCondition condition)
    {
        isExcessEnable = isExcessEnable | condition.isExcessEnabled();
        isOverRangeEnable = isOverRangeEnable | condition.isOverRangeEnabled();
        conditions.put(condition.getCompositionCode(),condition);
    }

    public void remove(String compositionCode)
    {
        conditions.remove(compositionCode);
    }

    public DataExcessCondition get(String compositionCode) {
        return conditions.get(compositionCode);
    }

    /**
     * @return 将对象转换为Json
     */
    public String toJosn()
    {
        return JSONObject.toJSONString(this,true);
    }

    /**
     * 由json字符串获取报警条件对象
     * @param json json字符串
     * @return 报警条件对象
     */
    public static LimberAlarmCondition fromJson(String json)
    {
        return JSONObject.parseObject(json,LimberAlarmCondition.class);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        LimberAlarmCondition condition = (LimberAlarmCondition) o;
        return conditions.equals(condition.conditions);
    }

    @Override
    public int hashCode() {
        return Objects.hash(conditions);
    }
}
